package cn.gjxblog.service.impl;

import cn.gjxblog.common.utils.UUIDUtils;
import cn.gjxblog.dao.CommentMapper;
import cn.gjxblog.model.entity.Comment;
import cn.gjxblog.model.entity.CommentExample;
import cn.gjxblog.service.CommentService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 作用:
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2019/1/19 16:51
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private CommentMapper commentMapper;

    @Override
    public void addHouseComment(String houseId, String content, String userId) {
        Comment comment = new Comment();
        comment.setHouseId(houseId);
        comment.setContent(content);
        comment.setUserId(userId);
        comment.setType(1);
        addComment(comment);
    }


    @Override
    public void addBlogComment(String blogId, String content, String userId) {
        Comment comment = new Comment();
        comment.setBlogId(blogId);
        comment.setContent(content);
        comment.setUserId(userId);
        comment.setType(2);
        addComment(comment);
    }

    private void addComment(Comment comment) {
        comment.setId(UUIDUtils.get32LUUid());
        comment.setCreateTime(new Date());
        commentMapper.insertSelective(comment);
    }

    @Override
    public List<Comment> queryHouseComments(String houseId, int size) {
        PageHelper.startPage(0,size);
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andHouseIdEqualTo(houseId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        return comments;
    }

    @Override
    public List<Comment> queryBlogComments(String blogId, int size) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andBlogIdEqualTo(blogId);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        return comments;
    }
}
